package net.alcuria.ball;

public class Snapshot {
	// Class that returns the tiles to be rendered

	// variables and their default values
	int x = 0;
	int y = 0;
	int viewX = 544;
	int viewY = 320;
	Map map = Assets.map;

	// calculated
	int numTilesX = 18;
	int numTilesY = 11;
	int tileWidth = map.tileWidth;
	int mapWidth = map.width;
	int mapHeight = map.height;

	// public (lol)
	public int left = 0;
	public int right = 18;
	public int top = 0;
	public int bottom = 11;
	public int dispX = 0;
	public int dispY = 0;

	public int screenX, screenY, diffY;

	public int pixLeft = 0;
	public int pixTop = 0;


	public Snapshot(int x, int y, int viewX, int viewY, Map map){
		this.x = x;
		this.y = y;
		this.viewX = viewX;
		this.viewY = viewY;
		this.map = map;

		tileWidth = map.tileWidth;

		numTilesX = viewX / tileWidth + 1;
		numTilesY = viewY / tileWidth + 1;

		left = x / tileWidth;
		right = left + numTilesX;

		top = y / tileWidth;
		bottom = top + numTilesY;

		dispX = (x % tileWidth) * (- 1);
		dispY = (y % tileWidth) * (- 1);

		pixLeft = left * tileWidth - dispX;
		pixTop = top * tileWidth - dispY;
	}

	public void move(int newX, int newY){

		// bounds checking 
		newX = newX < 0 ? 0 : newX;
		newY = newY < 0 ? 0 : newY;
		newX = newX > mapWidth*tileWidth-viewX ? mapWidth*tileWidth : newX;
		newY = newY > mapHeight*tileWidth-viewY ? mapHeight*tileWidth : newY;

		this.x = newX;
		this.y = newY;

		left = Math.min(x / tileWidth, mapWidth - numTilesX);
		right = left + numTilesX;

		top = Math.min(y / tileWidth, mapHeight - numTilesY);
		bottom = top + numTilesY;

		dispX = (x % tileWidth) * (- 1);
		dispY = (y % tileWidth) * (- 1);

		pixLeft = left * tileWidth - dispX;
		pixTop = top * tileWidth - dispY;
	}

	public void update(float playerX, float playerY, boolean onSurface) {
		// handle camera update
		screenX = (int) (playerX - x);
		screenY = (int) (playerY - y);
		diffY = 0;

		if(onSurface){
			diffY = screenY - 180;
			if (diffY < 0){
				diffY = Math.max(-7, diffY);
			} else {
				diffY = Math.min(7, diffY);
			}
		} else {
			if (screenY < 60){
				diffY = screenY - 60;
				diffY = Math.max(-20, diffY);
			}
			else if (screenY > 250){
				diffY = screenY - 250;
				diffY = Math.min(20, diffY);
			}
		}
		move(x, y+diffY);
		move((int) playerX-272, y);

	}

}
